package com.atgm.mall.gmmailcoupon.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.atgm.common.utils.GmMallUtils;
import com.atgm.common.utils.PageUtils;
import com.atgm.common.utils.Query;
import com.atgm.common.utils.R;
import com.atgm.mall.gmmailcoupon.dao.GameGoodsSaleDao;
import com.atgm.mall.gmmailcoupon.entity.GameGoodsSaleEntity;
import com.atgm.mall.gmmailcoupon.feign.ProductFeignService;
import com.atgm.mall.gmmailcoupon.feign.vo.FeignGameGoodsEntity;
import com.atgm.mall.gmmailcoupon.service.GoodsSaleService;
import com.atgm.mall.gmmailcoupon.vo.resp.GameGoodsSaleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class GoodsSaleServiceImpl extends ServiceImpl<GameGoodsSaleDao, GameGoodsSaleEntity> implements GoodsSaleService {

    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GameGoodsSaleEntity> page = this.page(
                new Query<GameGoodsSaleEntity>().getPage(params),
                new QueryWrapper<GameGoodsSaleEntity>().orderByDesc("id")
        );
        PageUtils pageUtils = new PageUtils(page);
        List<GameGoodsSaleEntity> records = page.getRecords();

        //得到商品id列表
        List<Long> goodsIds = records.stream().map(GameGoodsSaleEntity::getGameId).collect(Collectors.toList());
        //获取商品信息
        R r = productFeignService.getGoodsInfo(goodsIds);
        List<FeignGameGoodsEntity> data = r.getData(new TypeReference<List<FeignGameGoodsEntity>>() {});
        log.info("data : {}", data);

        if (GmMallUtils.isEmpty(data)) {

        }else {
            List<Long> feignGoodsIds = data.stream().map(FeignGameGoodsEntity::getGameId).collect(Collectors.toList());

            List<GameGoodsSaleVo> goodsSaleVos = new ArrayList<>();
            //装填返回值
            records.stream().forEach(goodsSaleEntity -> {
                GameGoodsSaleVo goodsSaleVo = new GameGoodsSaleVo();
                BeanUtils.copyProperties(goodsSaleEntity, goodsSaleVo);
                if (feignGoodsIds.contains(goodsSaleEntity.getGameId())) {
                    goodsSaleVo.setGameGoodsName(data.stream().filter(feignGoodsEntity -> feignGoodsEntity.getGameId().equals(goodsSaleVo.getGameId())).findAny().get().getGameName());
                    goodsSaleVo.setDefaultImg(data.stream().filter(feignGoodsEntity -> feignGoodsEntity.getGameId().equals(goodsSaleVo.getGameId())).findAny().get().getDefaultImg());
                    goodsSaleVo.setPrice(data.stream().filter(feignGoodsEntity -> feignGoodsEntity.getGameId().equals(goodsSaleVo.getGameId())).findAny().get().getPrice());
                }
                goodsSaleVos.add(goodsSaleVo);
            });
            BeanUtils.copyProperties(records, goodsSaleVos);
            log.info("size : {}", goodsSaleVos.size());

        pageUtils.setList(goodsSaleVos);
        }
        return pageUtils;
    }

    @Override
    public List<GameGoodsSaleEntity> queryInfoByCatIds(List<Long> goodsIds) {
        QueryWrapper<GameGoodsSaleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("game_id", goodsIds);
        List<GameGoodsSaleEntity> list = baseMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public GameGoodsSaleEntity queryGoodsSaleInfoByGoodsId(Long goodsId) {
        return baseMapper.selectOne(new QueryWrapper<GameGoodsSaleEntity>().eq("game_id", goodsId));
    }

    @Override
    public List<Long> querySaleGameIds(Map<String, Object> params) {
        IPage<GameGoodsSaleEntity> iPage = this.page(new Query<GameGoodsSaleEntity>().getPage(params),
                new QueryWrapper<GameGoodsSaleEntity>().orderByDesc("seckill_percentage"));
        List<GameGoodsSaleEntity> records = iPage.getRecords();
        if (GmMallUtils.isEmpty(records)) {
            return null;
        }
        return records.stream().map(GameGoodsSaleEntity :: getGameId).collect(Collectors.toList());
    }
}
